

class ChatClient
  attr_accessor :chat_panel,:shows_with,:chat_list,:chat_output,:chat_input,:ext_name,:shown_default
  def initialize(gui)
        @ext_name = "ChatClient"
#         @shows_with = 'mushcontent'
        @shown_default = 0
        @gui = gui
# array = ["<OOC>"] and com list player is in
	@chat_panel = Wx::Panel::new @gui
	@chat_input = Wx::TextCtrl::new @chat_panel
        $history[@chat_input]=History::new
	@chat_output = Wx::TextCtrl::new @chat_panel,:style=>Wx::TE_MULTILINE|Wx::TE_RICH
	@chat_list = Wx::ListBox::new @chat_panel
	@chat_panel.set_sizer Wx::StaticBoxSizer::new(Wx::HORIZONTAL,@chat_panel,"Chat Client (page control)")
	siz = Wx::BoxSizer::new Wx::VERTICAL
	siz.add @chat_output,Wx::ALL|Wx::GROW,Wx::EXPAND
	siz.add @chat_input,0,Wx::EXPAND
	@chat_panel.sizer.add siz,Wx::GROW|Wx::ALL,Wx::EXPAND
	@chat_panel.sizer.add @chat_list,Wx::ALL|Wx::GROW,Wx::EXPAND
        set_evts
        @gui.add_ext(self,@chat_panel)
        start if @gui.online?
        if !@gui.online?
          @chat_output.value = "No server detected.\nConnect to a server then\nPress Ctrl+E in the field below to activate client"
          @chat_input.value = "@gui.exts['#{@ext_name}'].start"
        end
  end
def find_syntax(widget)
  arr = []
  query = widget.get_selections
# p query
# exit
  query.each {|sel|
#     case sel

#       when $connected_players.index(sel)
       arr << "page "+$connected_players[sel]+" ="
#       when "<OOC>"
#        arr << "!"
#         when coms.index(sel)
#       end
  }
# p sel;exit
  return arr
#  p arr
end
  def set_evts
	@chat_input.evt_key_up {|e|
		p e
		if ( e.get_modifiers == Wx::MOD_CONTROL )
			p e.get_key_code
			case e.get_key_code
                                when 80
                                  @chat_input.value = $history[@chat_input].get_prev
                                when 78
                                  @chat_input.value = $history[@chat_input].get_next
				when 83
					p "EE"
					pre = find_syntax(@chat_list)
					pre.each {|q|
						p q
						@client.socket.puts q+" "+@chat_input.value
					}
					hour = Time.now.hour
					if hour < 12
						hour = "0#{hour}"
					else
						hour = hour.to_s
					end
					min,sec = Time.now.min.to_s,Time.now.sec.to_s
					str = "[#{hour}:#{min}:#{sec}]: "
					@chat_output.append_text "#{str}You send: "+@chat_input.value+"\n"
          				$history[@chat_input].enter @chat_input.value
                                        @chat_input.value = ""
                                when 69
                                        $history[@chat_input].enter @chat_input.value
                                        safe_eval @chat_input.value
                                        @chat_input.value = ""
                                     
			else
				e.skip
			end
		else
			e.skip
		end
	}
  end
  def start
	array = []
	@client = $client.client
	@client.send_wait << ""
	dbrefs = @client.recieve_data("th #{@client.get_header}%r[lwho()]%r#{@client.get_footer}",0)
	dbrefs.split(" ").each {|dbref|
	@client.send_wait << ""
	name = @client.recieve_data("th #{@client.get_header}%r[name(#{dbref})]%r#{@client.get_footer}",0)
	array << name.gsub("\r\n",'')
	}
	$connected_players = array 
	@chat_list.insert_items(array,0)
	@im_regexps = [/^(.*) pages\: (.*)/,
	/^From afar\, (.*)/,
	/^You page/,
	/^Long distance to/]

	@im_proc = proc do |reg,match_data,full|
        	hour = Time.now.hour
	        if hour < 12
			hour = "0#{hour}"
		else
			hour = hour.to_s
		end
		min,sec = Time.now.min.to_s,Time.now.sec.to_s
		str = "[#{hour}:#{min}:#{sec}]: "
		if @im_regexps.index(reg) == 0
			str << "#{match_data[1]}: #{match_data[2]}"
		elsif @im_regexps.index(reg) == 1
			str << "***#{match_data[1]}"
		else
			#mute it
			str = ""
		end
		p reg
		p match_data
		p full
		@chat_output.append_text(str+"\n") unless str == ""
		p full
	end
	
	@client.register_regexp @im_regexps,@im_proc
	s = <<EOB
	=========================================
	Brief file for Chat
	------------------------------------------------------------------------------------------------------
	Chat is a simple central ooc conversation 'application'
	When initialized it provides a 'list' of players see-able
	to you using the configured [*]who() function
	as well as if configured: OOC and any com channels you
	have joined
	------------------------------------------------------------------------------------------------------
	Ansi output is not implemented ATM
	You may send any mu* code as it will be sent to the server
	You may pose and semi pose as normal
	------------------------------------------------------------------------------------------------------
	The 'list' is able to select multiple targets
	Ctrl+s in the entry field sends to whateer is selected in 
	the list
	You may mix players (page) OOC and any com channel as 
	selected 
	=========================================
EOB
    end
  end

ChatClient::new self
# @gui.briefsObject.add_brief "Chat",s